package com.tianhao.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.tianhao.common.entity.HzeroRequestParam;
import com.tianhao.common.entity.ResponsePayloadDTO;
import com.tianhao.common.service.HzeroInterfaceInvokeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: tianhao.luo@hand-china.com 2021/3/24  17:51
 */
@Service
public class HzeroInterfaceInvokeServiceImpl implements HzeroInterfaceInvokeService {

    private static final String ACCESS_TOKEN="access_token";
    private static final String CONTENT_TYPE="Content-Type";
    private static final String APPLICATION_JSON_CHARSET="application/json;charset=UTF-8";
    private static final String APPLICATION_JSON="application/json";

    private static final Logger LOGGER = LoggerFactory.getLogger(HzeroInterfaceInvokeServiceImpl.class);

    /**
     * 获取token的地址
     */
    String tokenUrl = "http://10.185.62.161:8080/oauth/oauth/token?grant_type=client_credentials&client_id=7fp7ihuqj6&client_secret=9pcexw4hvr7d26swc1k3&source_type=web&device_id=uuid";

    /**
     * 业务接口请求地址
     */
    String interfaceUrl = "http://10.185.62.161:8080/hitf/v1/rest/invoke?namespace=HZERO&serverCode=ASSET_PURCHASE_ORDERS&interfaceCode=ADD_OR_UPDATE";
    String interfaceUrl2 = "http://10.185.62.161:8080/apfm/v1/0/file-upload-logs/asset/upload-file?assetNum=10";



    @Override
    public ResponsePayloadDTO invokeHzInterface(Object param) {
        ResponseEntity<ResponsePayloadDTO> entity = getEntity(param);
        return entity.getBody();
    }


    private ResponseEntity<ResponsePayloadDTO> getEntity(Object param) {
        //使用restTemplate来进行http post请求调用

        RestTemplate restTemplate = new RestTemplate();
        //获取token
        ResponseEntity<Map> exchange = restTemplate.exchange(tokenUrl, HttpMethod.POST, null, Map.class);
        Map body = exchange.getBody();
        String accessToken = ((String) body.get(ACCESS_TOKEN));

        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put(CONTENT_TYPE, APPLICATION_JSON_CHARSET);
        //将token放入请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setBearerAuth(accessToken);

        //接口请求体
        HzeroRequestParam hzeroRequestParam = new HzeroRequestParam();
        //设置mediaType
        hzeroRequestParam.setMediaType(APPLICATION_JSON);
        hzeroRequestParam.setHeaderParamMap(headerMap);
        //构造接口请求体
        //注意转化为json串时，需要将时间格式化
        hzeroRequestParam.setPayload(JSON.toJSONStringWithDateFormat(param,"yyyy-MM-dd hh:mm:ss"));
        System.out.println(JSON.toJSONString(hzeroRequestParam));//
        //将接口请求体和请求头合并
        HttpEntity<HzeroRequestParam> entity = new HttpEntity<>(hzeroRequestParam, httpHeaders);
        ResponseEntity<ResponsePayloadDTO> exchange1 = null;
        try {
            //开始执行post调用
//            exchange1 = restTemplate.exchange(interfaceUrl, HttpMethod.POST, entity, ResponsePayloadDTO.class);
        } catch (RestClientException e) {
            LOGGER.error(e.getMessage());
        }
        return exchange1;
    }
}
